from django.shortcuts import redirect, render
from django.contrib.auth.forms import UserCreationForm  # Django内置的注册表单
from django.contrib.auth import login as auth_login
from .forms import SignUpForm


# Create your views here.

def signup(request):
    """登录界面
    """
    if request.method == "POST":
        # 改用我们扩展后的SignUpForm
        # form = UserCreationForm(request.POST)
        form = SignUpForm(request.POST)
        if form.is_valid():
            user = form.save()  # 创建一个User实例
            auth_login(request, user)  # 手动验证用户
            return redirect('home')
    else:
        # form = UserCreationForm()
        form = SignUpForm()
    return render(request, 'signup.html', {'form': form})
